package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 1.封装sql语句
 * 2.调用JDBC方法
 * 3.查询调用query，新增，修改，删除调用update
 * 4.封装结果集
 * 5.重点:要学会单表的新增，修改，删除，查询的sql语句
 */
public class UserImpl implements UserDao {
    /**
     * @param userModel
     * @return
     */
    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        UserModel model = null;
        String sql = "select * from user where " +
                "username='" + userModel.getUsername() + "' and password='" + userModel.getPassword() + "' ";
//        ResultSet是数据库查询后得到的结果集
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                model = new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public Integer add(UserModel userModel) {
        String sql = "";
        return JDBC.update(sql);
    }

    @Override
    public Integer update(UserModel userModel) {
        return null;
    }

    @Override
    public Integer delete(UserModel userModel) {
        return null;
    }

    @Override
    public UserModel findById(UserModel userModel) {
        return null;
    }

    public List<UserModel> findAll(UserModel userModel) {
        String sql = "select * from user where username like '%" + userModel.getUsername() + "%'";
        ResultSet resultSet = JDBC.query(sql);
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel model = new UserModel();
                model.setUsername(resultSet.getString("username"));
                model.setId(resultSet.getInt("id"));
                model.setPhone(resultSet.getString("phone"));

                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Integer getCount(UserModel userModel) {
        return null;
    }
}
